<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 *游娃娃分享平台上游接口库封装类
 * 接口日志类
 * @author moon 2017.05.11
 */
class Apilog{
    /**
     * @param $db 日志数据库实例
     * @param $CI 框架类实例
     */
    private $db,$CI;

    /**
     * 构造函数
     * @param null
     * @return null
     * @author moon 2017.05.11
     */
    public function __construct()
    {
        $this->CI = & get_instance();
        $this->db = $this->CI->load->database("apilog",true);
    }
    /**
     * 日志写入方法
     * @param $type接口分类
     * @param $rqtimestamp接口请求时间戳
     * @param $rptimestamp接口响应时间戳
     * @param $actionid请求的商户id
     * @param $orderid请求的订单号(数字形式)
     * @param $msg响应的描述信息
     * @param $request请求数据包
     * @param $response响应的数据包
     * @return null
     * @author moon 2017.05.11
     */
    public function addlog($type,$rqtimestamp,$rptimestamp,$actionid=0,$orderid=0,$msg='',$request='',$response=''){
        $ip = ip2long($this->CI->input->ip_address());
        $sub_table_id = intval(substr($orderid,-1,1)) % 10;
        $table_name = "apilog_".$sub_table_id;

        $data = array();

        $data['type'] = intval($type);
        $data['rqtimestamp'] = intval($rqtimestamp);
        $data['rptimestamp'] = intval($rptimestamp);
        $data['ip'] = $ip;
        $data['actionid'] = intval($actionid);
        $data['orderid'] = $orderid;
        $data['msg'] = $msg;
        $data['request'] = $request;
        $data['response'] = $response;
        $sub_table_id = $sub_table_id == 0?10:$sub_table_id;
        $this->db->query("set session auto_increment_offset=$sub_table_id");
        $this->db->query("set session auto_increment_increment=10");
        $this->db->insert($table_name,$data);
    }
}